﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jzy.Model;
using Jzy.BLL.Mail;
using Jzy.Core.Common.Web;

using Jzy.Model.Mail;
using Frame.Lib.Utilities;
using Frame.Lib.Common;
using Frame.Lib.Enums;

public partial class WebMail_Account_Edit :PageEdit
{
    private string AccountID
    {
        get
        {
            return StrUtil.DecryptUrl(Request["ID"]);
        }
    }

    private WebMailAccountBO _mailAccount;
    protected void Page_Load(object sender, EventArgs e)
    {
        _mailAccount = new WebMailAccountBO(AppSession.UserID);
        if (!IsPostBack)
        {
            if (AccountID != null)
            {
                DispInfo();
            }
        }
        AddAttr();
    }

    private void AddAttr()
    {
        SmtpAuthenicate.Attributes.Add("onclick", "click1(this," + rad1.ClientID + "," + rad2.ClientID + ");");
        rad1.Attributes.Add("onclick", "click2(this," + rad2.ClientID + ");");
        rad2.Attributes.Add("onclick", "click2(" + rad1.ClientID + ",this);");
    }

    private void DispInfo()
    {
        WebMailAccountInfo info = _mailAccount.GetInfo(AccountID);
        EntityUtil.SetControl(pnlMain, info);

        Pop3Pwd.Attributes["value"] = info.Pop3Pwd;
        SmtpPwd.Attributes["value"] = info.SmtpPwd;

        if (info.SmtpAuthenicate == false)
        {
            SmtpAuthenicate.Checked = true;
            JSUtil.ExcuteJS("document.getElementById('" + SmtpAuthenicate.ClientID + "').click();","tmp");
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {

        if (_mailAccount.ExistField(TableField.MailAddr, MailAddr.Text, AccountID))
        {
            JSUtil.Alert("该邮件地址已存在，请重新填写");
            return;
        }
        WebMailAccountInfo info = _mailAccount.GetInfo(AccountID);
        EntityUtil.SetEntity(pnlMain, info);
        info.UserID = AppSession.UserID;
        if (rad1.Checked)
        {
            info.SmtpUser = info.Pop3User;
            info.SmtpPwd = info.Pop3Pwd;
        }
        info.ServerType = (int)Jzy.Model.Mail.MailServerType.POP3;

        _mailAccount.Save(info);
        JSUtil.RefreshView();
    }
}
